﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data.Query.Clauses;
using BlueCMS.Core.Data.Query.Columns;
using BlueCMS.Core.Data.Query.Generator;

namespace BlueCMS.Providers.DataProviders.MsSql2005Provider
{
    public class QueryGenerator : QueryGeneratorBase
    {
        public override string ParameterToken
        {
            get { return "@"; }
        }

        public override string StartingQuoteToken
        {
            get { return "["; }
        }

        public override string EndingQuoteToken
        {
            get { return "]"; }
        }

        protected override void GenerateSelectStatement(SelectClause clause, bool distinct, int limit, int offset, StringBuilder sb)
        {
            sb.Append("SELECT ");

            if (distinct)
            {
                sb.Append("DISTINCT ");
            }

            if (limit > 0)
            {
                sb.Append("TOP ");
                sb.Append(limit);
                sb.Append(" ");
            }

            if (clause.SelectColumns.Count == 0)
            {
                sb.Append("*");
            }
            else
            {
                foreach (SelectColumn column in clause.SelectColumns)
                {
                    GenerateSelectColumn(column, sb);
                    sb.Append(", ");
                }
                sb.Remove(sb.Length - 2, 2);
            }
        }
    }
}
